﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Elight.Entity.Ex;
using Elight.Logic.Ex;
using Elight.Utility;
using Elight.Utility.Files;
using Elight.Utility.Format;
using Elight.Utility.ResponseModels;
using Elight.WebUI.Controllers;
using Elight.WebUI.Filters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Hosting;
using Elight.Utility.Extension;

namespace Elight.WebUI.Areas.Ex.Controllers
{
    [Area("Ex")]
    [LoginChecked]
    public class ImgController : BaseController
    {
        private readonly IHostingEnvironment _hostingEnvironment;
        private ExImgLogic exImgLogic;
        public ImgController(IHostingEnvironment hostingEnvironment)
        {
            exImgLogic = new ExImgLogic();
            _hostingEnvironment = hostingEnvironment;
        }
        [HttpGet, AuthorizeChecked]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost, AuthorizeChecked]
        public ActionResult Index(int pageIndex, int pageSize, string keyWord)
        {
            int totalCount = 0;
            List<ExImg> pageData = exImgLogic.GetList(pageIndex, pageSize, keyWord, ref totalCount);
            if (pageData != null && pageData.Count > 0)
            {
                foreach (var item in pageData)
                {
                    item.ImagePath = ConstUtils.Url + item.ImagePath;
                }
            }
            var result = new LayPadding<ExImg>()
            {
                result = true,
                msg = "success",
                list = pageData,
                count = totalCount//pageData.Count
            };
            return Content(result.ToJson());
        }

        [HttpGet, AuthorizeChecked]
        public ActionResult Form()
        {
            return View();
        }
        [HttpPost]
        public ActionResult GetForm(string primaryKey)
        {
            ExImg entity = exImgLogic.GetForm(primaryKey);
            entity.ImagePath = ConstUtils.Url + entity.ImagePath;
            return Content(entity.ToJson());
        }

        [HttpPost, AuthorizeChecked]
        public ActionResult Form(ExImg model)
        {
            model.ImagePath = model.ImagePath.Replace(ConstUtils.Url, "");
            if (string.IsNullOrEmpty(model.Id))
            {  
                //新增轮播图信息
                var item = exImgLogic.Insert(model).ToString();
                return item != null ? Success() : Error();
            }
            else
            { 
                //更新轮播图信息
                int row = exImgLogic.Update(model);
                return row > 0 ? Success() : Error();
            }
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadImage()
        {
            try
            {
                var file = Request.Form.Files[0];
                string webRootPath = _hostingEnvironment.WebRootPath;///wwwroot 
                HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");//图片上传跨域
                string Logo = My_Upload.Image_Thumb(Request.Form.Files[0], "Enterprise_Logo");
                return Success("上传成功。", ConstUtils.Url + Logo);
            }
            catch (Exception exp)
            {
                return Success("上传失败。", exp.Message);
            }
        }

        [HttpPost, AuthorizeChecked]
        public ActionResult Delete(string primaryKeys)
        {
            //多图片删除
            int row = exImgLogic.Delete(primaryKeys.ToStrArray());
            return row > 0 ? Success() : Error();
        } 
    }
}
